//
//  GPSetCookies.m
//  gold-pointing
//
//  Created by CogerHuang on 16/7/21.
//  Copyright © 2016年 陈弘根. All rights reserved.
//

#import "GPSetCookies.h"

@implementation GPSetCookies
+(void)setCookieWithVersion:(NSInteger )version andName:(NSString *)name andValue:(NSString *)value andDomain:(NSString *)domain{
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie *tempCookie in cookies) {
        if ([tempCookie.name isEqualToString:name]&&[tempCookie.domain isEqualToString:domain]) {
            [cookieStorage deleteCookie:tempCookie];
        }
    }
    NSMutableDictionary *properties = [NSMutableDictionary dictionary];
    [properties setObject:@(version) forKey:NSHTTPCookieVersion];
    [properties setObject:name forKey:NSHTTPCookieName];
    [properties setObject:value forKey:NSHTTPCookieValue];
    [properties setObject:domain forKey:NSHTTPCookieDomain];
    [properties setObject:@"/" forKey:NSHTTPCookiePath];
    NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
@end
